local skill = fk.CreateSkill {
  name = "lvshi__julie",
  tags = {"Turn"}
}

local U = require "packages/utility/utility"
local ls = require "packages/lvshi/utility"

Fk:loadTranslationTable{
  ["lvshi__julie"] = "聚烈",
  ["@lvshi__julie"] = "聚烈",
  [":lvshi__julie"] = "回合技，当你需要使用或打出一张基本牌时，你可展示任意张手牌，若花色数量多于上次展示，你视为使用或打出之，否则你可分配这些牌给任意名角色各一张，然后你摸等量牌。",
  ["#lvshi__julie"] = "你可展示任意张手牌，视为使用一张基本牌",
  ["#lvshi__julie-get"] = "你可以获得%src展示的一张牌",
  ["#lvshi__julie-give"] = "你可以分配这些牌",

  ["$lvshi__julie1"] = "(视为杀)请君振炎汉之武运，除篡国之逆贼。",
  ["$lvshi__julie2"] = "(视为闪)杀出重围，成王者霸业！",
  ["$lvshi__julie3"] = "(视为桃)仁德之君，则所向披靡也！",
  ["$lvshi__julie4"] = "(视为酒)我将丹心酿烈酒，且取一觞慰风尘。",
  ["$lvshi__julie5"] = "(视为梨)仁施天下，德济众生。",
  ["$lvshi__julie6"] = "(视为毒)卿乃治国之股肱，国贼当前，可欲诛之？",
}

skill:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@lvshi__julie", 0)
end)

skill:addEffect("viewas", {
  mute = true,
  pattern = ".|.|.|.|.|basic",
  prompt = "#lvshi__julie",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(skill.name, all_names, nil, {})
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = function (self, player, to_select, selected)
    return table.contains(player:getCardIds("h"), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards == 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skill.name
    --self.cost_data = cards
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    player:showCards(use.card.fake_subcards)
    local suits = player:getMark("@lvshi__julie")
    local n = ls.countSuits(use.card.fake_subcards)
    room:setPlayerMark(player, "@lvshi__julie", n)
    if n > suits then
      local i = 1
      if use.card.trueName == "jink" then i = 2
      elseif use.card.trueName == "peach" then i = 3
      elseif use.card.trueName == "analeptic" then i = 4
      elseif use.card.trueName == "pear" or use.card.trueName == "scroll" then i = 5
      elseif use.card.trueName == "poison" then i = 6
      end 
      room:notifySkillInvoked(player, skill.name, "support")
      player:broadcastSkillInvoke(skill.name, i)
    else
      local cards = table.simpleClone(use.card.fake_subcards)
      local ret = room:askToYiji(player, {
        cards = cards,
        min_num = 1,
        max_num = #cards,
        single_max = 1,
        targets = room:getOtherPlayers(player),
        skill_name = skill.name,
        prompt = "#lvshi__julie-give",
        cancelable = false,
      })
      local count = 0
      for p, _ in pairs(ret) do
        if #ret[p] > 0 then
          count = count + 1
        end
      end
      if count > 0 then
        player:drawCards(count, skill.name)
      end
      return skill.name
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skill.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    --if response then return false end
    return player:usedSkillTimes(skill.name, Player.HistoryTurn) == 0
  end,
})

return skill